{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-1-7775a4f8db96>:9: DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).\n",
      "  sheet = wb.get_sheet_by_name(\"录入分录明细\")\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i=2,str(sheet.cell(x,4).value)\n",
      "i=3,str(sheet.cell(x,5).value)\n",
      "i=4,str(sheet.cell(x,5).value)\n"
     ]
    },
    {
     "ename": "FailSafeException",
     "evalue": "PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFailSafeException\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-7775a4f8db96>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     25\u001b[0m         \u001b[0mpag\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhotkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Enter'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m##回车\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     26\u001b[0m         \u001b[0mpag\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhotkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Enter'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m##回车\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 27\u001b[1;33m         \u001b[0mpag\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhotkey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Enter'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m##回车\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     28\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mf\"i={x},保存赁证\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     29\u001b[0m         \u001b[1;32mcontinue\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\ProgramData\\Anaconda3\\lib\\site-packages\\pyautogui\\__init__.py\u001b[0m in \u001b[0;36mwrapper\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m    583\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mfunctools\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwraps\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwrappedFunction\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    584\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mwrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 585\u001b[1;33m         \u001b[0mfailSafeCheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    586\u001b[0m         \u001b[0mreturnVal\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwrappedFunction\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    587\u001b[0m         \u001b[0m_handlePause\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"_pause\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\ProgramData\\Anaconda3\\lib\\site-packages\\pyautogui\\__init__.py\u001b[0m in \u001b[0;36mfailSafeCheck\u001b[1;34m()\u001b[0m\n\u001b[0;32m   1708\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mfailSafeCheck\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1709\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mFAILSAFE\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mtuple\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mposition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mFAILSAFE_POINTS\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1710\u001b[1;33m         raise FailSafeException(\n\u001b[0m\u001b[0;32m   1711\u001b[0m             \u001b[1;34m\"PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED.\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1712\u001b[0m         )\n",
      "\u001b[1;31mFailSafeException\u001b[0m: PyAutoGUI fail-safe triggered from mouse moving to a corner of the screen. To disable this fail-safe, set pyautogui.FAILSAFE to False. DISABLING FAIL-SAFE IS NOT RECOMMENDED."
     ]
    }
   ],
   "source": [
    "import pyautogui as pag  ##先用pip3 install python3-xlib，\n",
    "#再用pip3 install pyautogui进行安装\n",
    "import pyperclip\n",
    "import openpyxl\n",
    "pag.PAUSE = 0.5# pyautogui.PAUSE函数在pyautogui中含义是每一步停留时间间隔，\n",
    "#模块默认值是0.1秒，不过我用的财务软件反应不过来，因此延长了时间设置成了0.5秒。\n",
    "pag.FAILSAFE = True\n",
    "wb = openpyxl.load_workbook(\"凭证录入模块.xlsx\", data_only=True)\n",
    "sheet = wb.get_sheet_by_name(\"录入分录明细\")\n",
    "max_row = sheet.max_row# 读取excel中的最大行\n",
    "max_column=sheet.max_column##读取最大列\n",
    "pag.click(766, 365) # 增加凭证\")\n",
    "pag.hotkey('Enter')##回车 \n",
    "pag.hotkey('Enter')##回车 \n",
    "pag.hotkey('Enter')##回车 \n",
    "for x in range(2,max_row+1):\n",
    "    #pyperclip.copy(str(sheet.cell(x,1).value))##复制EXCEL中的摘要\n",
    "    #pag.hotkey('ctrl', 'v')##粘贴单元格\n",
    "    #pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "    #pag.typewrite(str(sheet.cell(x,2).value))##输入科目代码\n",
    "    #pag.hotkey('Tab')##热键Tab到科目代码栏 \n",
    "    if sheet.cell(x,3).value is None:\n",
    "        pag.click(755, 392) # 点击保存\n",
    "        pag.click(766, 365) # 增加凭证\")\n",
    "        pag.hotkey('Enter')##回车 \n",
    "        pag.hotkey('Enter')##回车 \n",
    "        pag.hotkey('Enter')##回车 \n",
    "        print(f\"i={x},保存赁证\")\n",
    "        continue\n",
    "    if sheet.cell(x,3).value==\"贷\":\n",
    "        pag.hotkey('Backspace')\n",
    "        #pag.click(680, 480 + (x%2)*40) ##点击摘要\n",
    "        pyperclip.copy(str(sheet.cell(x,1).value))##复制EXCEL中的摘要\n",
    "        pag.hotkey('ctrl','v')##粘贴单元格\n",
    "        pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        pag.typewrite(str(sheet.cell(x,2).value))##输入科目代码\n",
    "        pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        pag.typewrite(str(sheet.cell(x,5).value))##输入贷方\n",
    "        pag.hotkey('Enter')##回车\n",
    "        pag.hotkey('Backspace')\n",
    "        print(f\"i={x},str(sheet.cell(x,5).value)\")\n",
    "    else:\n",
    "        pag.hotkey('Backspace')\n",
    "        #pag.click(683, 486 + (x%2)*40) ##点击摘要\n",
    "        pyperclip.copy(str(sheet.cell(x,1).value))##复制EXCEL中的摘要\n",
    "        pag.hotkey('ctrl','v')##粘贴单元格\n",
    "        pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        pag.typewrite(str(sheet.cell(x,2).value))##输入科目代码\n",
    "        pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        pag.typewrite(str(sheet.cell(x,4).value))##输入借方\n",
    "        pag.hotkey('Enter')##回车\n",
    "        print(f\"i={x},str(sheet.cell(x,4).value)\")\n",
    "        ##    elif str(sheet.cell(x,3).value==\"借\"):\n",
    "        ##pag.hotkey('Tab')###回车 ##热键Tab到科目代码栏\n",
    "        ##pag.typewrite(str(sheet.cell(x,4).value))##输入贷方\n",
    "        #print(f\"i={x},str(sheet.cell(x,4).value)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
